package main.scripts;

import java.awt.Graphics;

import javax.swing.JOptionPane;

import main.bots.Bot;
import main.bots.Script;
import main.constants.Constants;
import main.hooks.RSNPC;
import main.listeners.PaintListener;
import main.listeners.ServerMessageListener;

@SuppressWarnings("serial")
public class Combat extends Script implements Bot, ServerMessageListener, PaintListener  {

	private boolean toEnd = false;
	
	public int npcId = 0;

	private String name = "";
	
	public int startLevel = 0;

	private int startExp = 0;

	private long lastLoop;

	private int testInt;
	
	long lastPaint = 0;

	@Override
	public String[] getAuthors() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public double getVersion() {
	
		return 1;
	}
@Override
public String getScriptName() {
	// TODO Auto-generated method stub
	return Constants.BOT_COMBAT_NAME;
}
	@Override
	public int loop() {
		if(lastLoop == 0) {
		lastLoop = System.currentTimeMillis();
		return 100;
		}
		debug("Last Loop: " + (System.currentTimeMillis() - lastLoop));
		lastLoop = System.currentTimeMillis();
		if(name == "")
			return 1000;
		if(isInCombat()) {
			debug("Already in combat");
			testInt = 4;
			return 1000;
		} else {
			if(testInt > 0) {
				testInt--;
				debug("Don't seem to be in combat..");
				return 500;
			} else {
			
		
		RSNPC toAttack = getClosestNpcToAttack(name);
		if(toAttack == null) {
			debug("Sorry, doesn't exist");
			return 1000;
		}
		attackNpc(toAttack);
		return 6000;
	}
		}
	}
	@Override
	public void onFinish() {
		System.out.println("Glad you had fun :)");

	}

	@Override
	public boolean onStart() {
		System.out.println("Starting combat bot");
		name = JOptionPane.showInputDialog(this, (new StringBuilder()).append("Enter in Npc NAME, EXACT"), "", 3).trim();
		return name != "";
	}

	@Override
	public boolean endCondition() {
		// TODO Auto-generated method stub
		return toEnd ;
	}

	@Override
	public String getCategory() {
		return "Combat";
	}

	@Override
	public void serverMessageRecieved(String message) {
	debug("Server Message: " + message);
		
	}
	@Override
	public boolean onRepaint(Graphics g) {
		if(g == null) {
			debug("Graphics are null!");
			return false;
		}
		if(lastPaint == 0) {
			lastPaint = System.currentTimeMillis();
			return false;
		}
		if(System.currentTimeMillis() - lastPaint < 3000)
			return false;
		if(startLevel == 0) {
			startLevel = getSkillExp(Constants.INTERFACE_SKILLS_CURRENT_LEVEL_ATTACK);
		}
		if(startExp == 0) {
			startExp = getSkillExp(Constants.INTERFACE_SKILLS_EXP_ATTACK);
		}
		g.drawString("Levels Gained: " + (getSkillExp(Constants.INTERFACE_SKILLS_CURRENT_LEVEL_ATTACK) - startLevel), 571, 261);
		g.drawString("Exp Gained: " + (getSkillExp(Constants.INTERFACE_SKILLS_EXP_ATTACK) - startExp), 571, 281);
		return true;
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "A combat script, version: " + getVersion();
	}
	

}
